#Rem
Header: Drag image class 
#End
Import mojo
Import agfx

#Rem
	summary: - Drag&Drop state NONE
#End
Const DRAG_NONE:Int = 0
#Rem
	summary: - Drag&Drop state START
#End
Const DRAG_START:Int = 1
#Rem
	summary: - Drag&Drop state END
#End
Const DRAG_END:Int = 2

#Rem
summary: TDragImage
#End
Class TDragImage
	
	Public
	#Rem
		summary: - user defined instance name
	#End
	Field name:String="noname"	
	
	#Rem
		summary: - user defined callback, when mouse up release
	#End
	Field OnDragEnd:TCallback

	#Rem
		summary: - user defined callback, when mouse move
	#End
	Field OnDrag:TCallback = New TCallback()
	
	#Rem
		summary: - position vector
	#End
	Field position:TVector2
	
	#Rem
		summary: - assigned image
	#End
	Field image:TImage	
	
	#Rem
		summary: - actual state
	#End
	Field dragState:Int=DRAG_NONE
	
	#Rem
		summary: - actual image position
	#End
	Field image_pos:TVector2 = New TVector2()

	Field dragOffset:TVector2 

	Private
	Field bDrag:Bool = False
	Field blend:Int= 0
	Field alpha:Float=1.0
	Field width:Int
	Field height:Int	
	Field drag_start_pos:TVector2	
	Field mouse_hit_pos:TVector2 
	
	'|---------------------------------------------------------------------------------------
	'| PUBLIC METHODS
	'|---------------------------------------------------------------------------------------
	
	Public 
	
	'|---------------------------------------------------------------------------------------
	'| CONSTRUCTOR
	'|---------------------------------------------------------------------------------------
	#Rem
		summary: - 
	#End
	Method New(n:String)
		Self.name = n
		Self.bDrag = False
	End
	
	'|---------------------------------------------------------------------------------------
	'| CREATE
	'|---------------------------------------------------------------------------------------
	#Rem
		summary: - 
	#End
	Method Create(name:String)
	
		Self.image = IMAGES.Find(name)
		
		Self.position = New TVector2(0,0)
		Self.dragOffset = New TVector2(0,0)
		Self.width = Self.image.w
		Self.height = Self.image.h
		Self.mouse_hit_pos = New TVector2(0,0)	
		
	End
	
	'|---------------------------------------------------------------------------------------
	'| UPDATE
	'|---------------------------------------------------------------------------------------
	#Rem
		summary: - update Drag&Drop
		add this to OnUpdate() App callback
	#End
	Method Update()
		

		If MOUSE.Hit() And Self.IsMouseInside() And Self.bDrag=False Then
			Self.mouse_hit_pos.Set(MOUSE.x,MOUSE.y)		
			Self.drag_start_pos = New TVector2 (0,0)
			Self.drag_start_pos.Set(Self.position)
			Self.bDrag=True	
			Self.dragState = DRAG_START 
			Print("HIT")
		End
		
		If Self.bDrag Then
		
			If MOUSE.Down() Then
				Local m_pos:TVector2 = New TVector2(MOUSE.x,MOUSE.y)			
				Self.dragOffset = VecSub(m_pos,mouse_hit_pos)	
				'Self.dragState = DRAG_NONE 			
				Self.OnDrag.Call()
				
			End				
			
			If MOUSE.Up() Then
				Self.position =  VecAdd(Self.position,Self.dragOffset)					
				Self.dragOffset.Set(0,0)
				Self.mouse_hit_pos.Set(0,0)
				Self.bDrag = False
				Self.dragState = DRAG_END 
				Self.OnDragEnd.Call()
				Print("DRAG END")
			End
		End		
		
	End
	
	'|---------------------------------------------------------------------------------------
	'| RESET drag state
	'|---------------------------------------------------------------------------------------
	#Rem
		summary: - reset state
	#End
	Method ResetState()
		Self.dragState = DRAG_NONE
	End
	
	'|---------------------------------------------------------------------------------------
	'| RESET position
	'|---------------------------------------------------------------------------------------
	Method TweenBackToStart()
		Self.position.Set(Self.drag_start_pos)
	End
	
	'|---------------------------------------------------------------------------------------
	'| DRAW
	'|---------------------------------------------------------------------------------------
	#Rem
		summary: - draw Drag&Drop
		add this call to OnRender() app callback
	#End
	Method Draw()
		
		
	
		if Self.bDrag Then			
			image_pos = VecAdd(Self.position,Self.dragOffset)	
		Else
			image_pos.Set(Self.position)		
		End
		
		
		'SetBlend (Self.blend)
		'SetAlpha (Self.alpha)
		Self.image.Draw(image_pos.x ,image_pos.y)
	
	End
	

	'|---------------------------------------------------------------------------------------
	'| PRIVATE METHODS
	'|---------------------------------------------------------------------------------------	
	Private
	
	Method IsMouseInside:Bool()
		If (MOUSE.x >= Self.position.x - Self.image.GetPivotX()) And 
		   (MOUSE.x <= (Self.position.x + (Self.width- Self.image.GetPivotX()))) And 
		   (MOUSE.y >= Self.position.y - Self.image.GetPivotY()) And 
		   (MOUSE.y <= (Self.position.y + (Self.height - Self.image.GetPivotY()))) Then
			Return True
		Else
			Return False
		End If
	End Method
End





